package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lvFF
 * @Date 2022/04/27/上午 11:18;
 * Description
 */
@RestController
@RequestMapping("dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        /**
        Page<Dish> dishPage = new Page<>(page, pageSize);
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        //添加排序条件
        queryWrapper.orderByDesc("update_time");

        Page<Dish> page1 = dishService.page(dishPage, queryWrapper);


        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);

        ArrayList<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : dishPage.getRecords()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //根据id查询分类对象
            Category category = categoryService.getById(dish.getCategoryId());
            //设置分类名称
            dishDto.setCategoryName(category.getName());
            //将dishDto添加到集合中
            dishDtoList.add(dishDto);
        }
        dishDtoPage.setRecords(dishDtoList);

        //拷贝第二部分数据
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

*/
        Page<DishDto> dishDtoPage = dishService.pageWith(page, pageSize, name);
        return R.success(dishDtoPage);
    }

    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
        dishService.saveFlaver(dishDto);
        return R.success("null-11");
    }


    /**
     * 删除和批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        dishService.removeByIds(ids);
        return R.success("删除成功");
    }

     @PostMapping("/status/{s}")
     public R<String> status(@PathVariable("s") Integer s,Long[] ids){
         log.info("s--------------------{}",s);
         log.info("ids------------------{}",ids);
         dishService.getByIdS(s,ids);
         //log.info("byId------------------{}",byId);
         return R.success("null-11");
     }

    /**
     * 根据id使当前 属性显示出来  回显修改数据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<DishDto> update(@PathVariable Long id) {
        DishDto byIdDishFlavor = dishService.getByIdDishFlavor(id);
        return R.success(byIdDishFlavor);
    }


    /**
     * 修改方法
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update2(@RequestBody DishDto dishDto) {
        dishService.updateById(dishDto);
        return R.success("修改成果");
    }

    /**
     * 添加套餐里面的菜品回显
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    public R<List<DishDto>> list(Long categoryId){
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(categoryId != null,"category_id",categoryId);
        //queryWrapper.eq("status",)
        queryWrapper.eq("status", 1);

        queryWrapper.orderByDesc("sort").orderByDesc("update_time");
        List<Dish> list = dishService.list(queryWrapper);
        //

        //
        ArrayList<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish : list) {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(dish,dishDto);

            QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dish_id",dish.getId());

            List<DishFlavor> list1 = dishFlavorService.list(queryWrapper1);

            dishDto.setFlavors(list1);

            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }

}
